package com.topone.Converter;

import com.google.gson.Gson;
import com.topone.Enum.ResultEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.OrderForm;
import com.topone.dataObject.OrderDetail;
import com.topone.dto.OrderDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/5,0005.
 */
@Slf4j
public class OrderForm2OrderDTOConvert {
    public static OrderDTO convert(OrderForm orderForm){

        Gson gson=new Gson();
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getOrderAmount());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        List<OrderDetail> orderDetailList=new ArrayList<>();
        OrderDetail orderDetail=new OrderDetail();
        try{
//            //将json字符串转换成list
//            orderDetailList=  gson.fromJson(orderForm.getCouponId(), new TypeToken<List<OrderDetail>>() {
//            }.getType());
           // OrderDetail orderDetail=gson.fromJson(orderForm.getCouponId(),OrderDetail.class);
            orderDetail.setCouponNum(orderForm.getCouponNum());
            orderDetail.setCouponId(orderForm.getCouponId());
            orderDetailList.add(orderDetail);
        }catch (Exception e){
            log.error("【对象转换】错误,String={}",orderForm.getCouponId());
            throw new FanZoneException(ResultEnum.PARAM_ERROR);
        }

        orderDTO.setOrderDetailList(orderDetailList);

        return orderDTO;
    }

}

